# -*- ecoding: utf-8 -*-
# @ModuleName: ddt_utli
# @Author: wangwei 
# @Time: 2023/10/23 14:28
import re

import openpyxl

from common import encry_decry
from get_project_path import project_path


def get_read_excel(filename, sheetname):
    read_excel = openpyxl.load_workbook(f"{project_path()}{filename}")
    sheet = read_excel[sheetname]
    return sheet


def get_all_exec_sheet(filename="/data/mtxshop_keyword.xlsx", sheetname='测试用例集合'):
    all_exec_sheet = []
    sheet = get_read_excel(filename=filename, sheetname=sheetname)
    rows = sheet.max_row
    for row in range(2, rows + 1):
        if sheet.cell(row, 2).value.lower() == 'y':
            all_exec_sheet.append(sheet.cell(row, 1).value)
    return all_exec_sheet


def get_global_variable(filename="/data/mtxshop_keyword.xlsx", sheetname='全局变量'):
    global_variable = {}
    sheet = get_read_excel(filename=filename, sheetname=sheetname)
    rows = sheet.max_row
    for row in range(2, rows + 1):
        variable_name = sheet.cell(row, 1).value
        variable_value = sheet.cell(row, 2).value
        global_variable[variable_name] = variable_value
    return global_variable


def get_all_api_default_params(filename="/data/mtxshop_keyword.xlsx", sheetname='接口默认参数'):
    api_default_params = {}
    sheet = get_read_excel(filename=filename, sheetname=sheetname)
    rows = sheet.max_row
    for row in range(2, rows + 1):
        variable_name = sheet.cell(row, 1).value
        variable_value = sheet.cell(row, 2).value
        api_default_params[variable_name] = variable_value
    return api_default_params


# def get_sheet_testcases(filename="", sheetname='添加购物车测试集合'):
#     all_sheet_testcases = {}
#     sheet = get_read_excel(filename=filename, sheetname=sheetname)
#     rows = sheet.max_row
#     column=sheet.max_column
#     for row in range(2, rows + 1):
#         casename = sheet.cell(row, 1).value
#         api_info=[]
#         for column in range(2,column+1):
#             cell_value=sheet.cell(row, column).value
#             if not cell_value:
#                 cell_value=""
#             api_info.append(cell_value)
#         if casename in all_sheet_testcases:
#             all_sheet_testcases[casename].append(api_info)
#         else:
#             all_sheet_testcases[casename] =[api_info]
#     return all_sheet_testcases


def get_sheet_testcases(filename="/data/mtxshop_keyword.xlsx", sheetname='添加购物车测试集合'):
    sheet = get_read_excel(filename=filename, sheetname=sheetname)
    rows = sheet.max_row
    columns = sheet.max_column
    all_sheet_cases = {}
    for row in range(2, rows + 1):
        testcase_name = sheet.cell(row, 1).value
        api_info = []
        for column in range(2, columns + 1):
            cell_value = sheet.cell(row, column).value
            if not cell_value:
                cell_value = ""
            api_info.append(cell_value)
        if testcase_name not in all_sheet_cases:
            all_sheet_cases[testcase_name] = [api_info]
        else:
            all_sheet_cases[testcase_name].append(api_info)
    return all_sheet_cases


def get_all_cases(filename="/data/mtxshop_keyword.xlsx"):
    sheet_cases = get_all_exec_sheet(filename)
    test_cases = []
    for sheet_case in sheet_cases:
        all_sheet_case = get_sheet_testcases(filename=filename, sheetname=sheet_case)
        for key, value in all_sheet_case.items():
            test_cases.append([key, value])
    return test_cases


def regx_sub(string_data: str, vars_dict: dict):
    # ${sellerHost}/seller/goods
    try:
        get_value = re.findall("\$\{(.*?)\}", string_data)[0]
        new_value = vars_dict.get(get_value)
        string_data = string_data.replace(f"${{{get_value}}}", new_value)
    except Exception as e:
        string_data = string_data
    return string_data


def regx_exec(string: str):
    # str='{"params":{"username":"shamo","password":"${{md5(abc123456)}}","captcha":"1512","uuid":"hjsdhdfhhf"}}'
    try:
        res_data = re.findall('\$\{\{(.*?)\((.*?)\)\}\}', string)
        function_name = res_data[0][0]
        function_param = res_data[0][1]
        if hasattr(encry_decry, function_name):
            func = getattr(encry_decry, function_name)
            res_new_data = func(function_param)
            string_data = string.replace(f"${{{{{function_name}({function_param})}}}}", res_new_data)
    except Exception as e:
        string_data = string
    return string_data


if __name__ == '__main__':
    # print(get_all_exec_sheet(""))
    # print(get_global_variable("/data/mtxshop_keyword.xlsx"))
    # print(get_all_api_default_params("/data/mtxshop_keyword.xlsx").get('买家登录'))
    # print(type(get_all_api_default_params("/data/mtxshop_keyword.xlsx").get('买家登录')))
    # # print(get_all_cases("/data/mtxshop_keyword.xlsx"))
    global_variable = get_global_variable()
    print(regx_sub('/seller/goods', global_variable))
    print(regx_exec('/seller/goods'))
    print(regx_sub('/seller/goods', global_variable))
    str = '{"params":{"username":"shamo","password":"","captcha":"1512","uuid":"hjsdhdfhhf"}}'
    print(regx_sub(str, global_variable))
    print(regx_exec(str))
    print(regx_sub(str, global_variable))


